package oop.ex3.crosswords;

import java.util.Comparator;

/**
 * File name: EntryComparator.java
 * Description: Compares CrosswordEntry objects by this order: length, alphabetically, overlaps,
 * x coordinate, y coordinate and orientation.
 * @author yossavi dawn16889
 */
public class EntryComparator implements Comparator<OverLapEntry>{
	
	/* (non-Javadoc)
	 * @see java.util.Comparator#compare(java.lang.Object, java.lang.Object)
	 */
	@Override
	public int compare(OverLapEntry o1, OverLapEntry o2) {
		
		// compares by length.
		if(o1.getEntry().getLength() != o2.getEntry().getLength())
			return o2.getEntry().getLength() - o1.getEntry().getLength();

		// compares by alphabetical order.
		if(o1.getEntry().getTerm().compareTo(o2.getEntry().getTerm())!=0)
			return o1.getEntry().getTerm().compareTo(o2.getEntry().getTerm());

		// compares by overlaps.
		if (o1.getOverlap()!=o2.getOverlap())
			return o2.getOverlap()-o1.getOverlap();

		// compares by x coordinate position.
		if(o1.getEntry().getPosition().getX()!=o2.getEntry().getPosition().getX())
			return o1.getEntry().getPosition().getX()-o2.getEntry().getPosition().getX();

		// compares by y coordinate position.
		if(o1.getEntry().getPosition().getY()!=o2.getEntry().getPosition().getY())
			return o1.getEntry().getPosition().getY()-o2.getEntry().getPosition().getY();

		// compares by orientation.
		if (o1.getEntry().getPosition().isVertical() && !o2.getEntry().getPosition().isVertical())
			return -1;
		if (!o1.getEntry().getPosition().isVertical() && o2.getEntry().getPosition().isVertical())
			return 1;
		
		//if they equal.
		return 0;
	}

}
